package com.example.project.webControlleer;

import com.example.project.dao.UserDAO;
import com.example.project.dao.WalletHistroyDAO;
import com.example.project.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("web")
public class WebChargeController {
    @RequestMapping("charge")
    public void charge(HttpSession session, HttpServletRequest request){
        UserDAO userDAO=new UserDAO();
        User user =(User) session.getAttribute("user");
        double money= userDAO.findTotalMoneyByID(user.getId());
        request.setAttribute("money",money);
    }
    @RequestMapping("moneyCharge")
    public String moneyCharge(double money,HttpSession session){
            User user =(User) session.getAttribute("user");
        UserDAO userDAO =new UserDAO();
       user.setTotalMoney(userDAO.findTotalMoneyByID(user.getId()));
        userDAO.charge(money,user);
        WalletHistroyDAO walletHistroyDAO =new WalletHistroyDAO();
        walletHistroyDAO.chargeHistory(user,money);
        session.setAttribute("user",user);
        return "redirect:/web/wallet";
    }
}
